/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.refinement;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author dk
 */
public class ValueInfoOfDP extends Hashtable<String, String> {

    public static int BOOLEAN = 0;
    public static int INT = 1;
    public static int STRING = 2;
    public static int DATARANGE = 3;
    public static int DATE = 4;
    private Vector standards;
    private int type;
    private Hashtable<String, ArrayList<String>> randValueTable = new Hashtable();

    public void putResourceAndValueTable(String r, String v) {
        if (randValueTable.containsKey(r)) {
            ArrayList<String> vset = randValueTable.get(r);
            if (!vset.contains(v)) {
                vset.add(v);
            }
        } else {
            ArrayList<String> vset = new ArrayList();
            vset.add(v);
            randValueTable.put(r, vset);

        }
    }

    public Hashtable<String, ArrayList<String>> getRandValueTable() {
        return randValueTable;
    }

//    private ArrayList<Triple> tset;
//    
//    public void setTripleSet(ArrayList<Triple> tripleSet){
//        this.tset = tripleSet;
//    }
//    
//    public ArrayList<Triple> getTripleSet(){
//        return tset;
//    }
    public void setType(int t) {
        this.type = t;
    }

    public int getType() {
        return type;
    }

    public Vector getStandard() {
        if (this.standards != null) {
            return this.standards;
        } else {
            this.standards = new Vector();
            if (this.type == ValueInfoOfDP.INT) {
                int high = 0;
                Enumeration<String> keys = this.keys();
                while (keys.hasMoreElements()) {
                    int value = Integer.parseInt(keys.nextElement());
                    if (value > high) {
                        value = high;
                    }
                }

                int low = (int) (high * 0.2);
                int mid = (int) (high * 0.5);
                int mid2 = (int) (high * 0.8);

//                this.standards.add(0);
                
                this.standards.add(low);                
                if (!standards.contains(mid)) {
                    this.standards.add(mid);
                }
                if (!standards.contains(mid2)) {
                    this.standards.add(mid2);
                }
            } else if (this.type == ValueInfoOfDP.STRING) {
                Enumeration<String> keys = this.keys();
                while (keys.hasMoreElements()) {
                    String value = keys.nextElement().toString();
                    this.standards.add(value);
                }
            } else if (this.type == ValueInfoOfDP.DATE) {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                Enumeration<String> keys = this.keys();
                try {
                    Date past = formatter.parse("2200-12-31");
                    Date latest = formatter.parse("1900-01-01");;
                    while (keys.hasMoreElements()) {
                        String value = keys.nextElement().toString();
                        Date vDate = formatter.parse(value);
                        if (vDate.before(past)) {
                            past=vDate;
                        }
                        if(vDate.after(latest)){
                            latest = vDate;
                        }                        
                    }
                    
                    long diff = latest.getTime() - past.getTime();
                    long diffday = diff / (24 * 60 * 60 * 1000);
                    int addDay = (int) (diffday * -0.02);
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(latest);
            //                    System.out.println(diff + " - "+addDay+" + "+ diffday);
                    if (diffday < 20) {
                        if(diffday > 3){
                            cal.add(Calendar.DATE, 5);
                            this.standards.add(formatter.format(cal.getTime()));
//                            System.out.println("1:"+formatter.format(cal.getTime()));
                        }else{
                            cal.add(Calendar.DATE, 1);
                            this.standards.add(formatter.format(cal.getTime()));
//                            System.out.println("2:"+formatter.format(cal.getTime()));
                        }
                    } else {
                        for (int i = 0; i < 25; i++) {
                            cal.add(Calendar.DATE, addDay);
                            this.standards.add(formatter.format(cal.getTime()));
//                            System.out.println("3:"+formatter.format(cal.getTime()));
                        }
                    }
                } catch (ParseException ex) {
                    Logger.getLogger(ValueInfoOfDP.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
            return standards;
        }
    }
}
